﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Outdoors.Helpers.Foursquare
{
    public class FoursquareVenue
    {
        public string VenueName { get; set; }
        public string Address { get; set; }
        public int Distance { get; set; }
        public int CheckIns { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public async static Task<List<FoursquareVenue>> GetFoursquareVenues(double latitude = double.NaN, double longitude = double.NaN, string categoryId = "")
        {
            var foursquareVenues = new List<FoursquareVenue>();

            HttpClient client = new HttpClient();
            string foursquareResultSearchUrl = FoursquareAPI.VenuesSearchUrl(latitude, longitude, categoryId);
            string foursquareResult = await client.GetStringAsync(foursquareResultSearchUrl);
            FoursquareData foursquareData = JsonConvert.DeserializeObject<FoursquareData>(foursquareResult);

            if (foursquareData.meta.code == 200)
            {
                foreach (var venue in foursquareData.response.venues)
                {
                    var foursquareVenue = new FoursquareVenue()
                    {
                        VenueName = venue.name.ToLower(),
                        Address = venue.location.address,
                        Distance = venue.location.distance,
                        CheckIns = venue.stats.checkinsCount,
                        Longitude = venue.location.lng,
                        Latitude = venue.location.lat
                    };

                    foursquareVenues.Add(foursquareVenue);
                }
            }

            return foursquareVenues;
        }
    }
}
